<template>
  <div class="app-container">
    <div class="label common-content">
      <!--      每一项标签-->
      <div class="detail">
        <div class="color-select">
          <el-color-picker
            v-model="color"
            show-alpha
            size="small"
            :predefine="predefineColors">
          </el-color-picker>
        </div>
        <!--        标题-->
        <div class="title">
          我的外号
        </div>
<!--        列表-->
        <div class="list">
          <el-tag
            :style="{backgroundColor:color}"
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="$event.target.blur"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button :style="{backgroundColor:color}" v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
        </div>
      </div>
      <!--      每一项标签-->
      <div class="detail">
        <div class="color-select">
          <el-color-picker
            v-model="color"
            show-alpha
            size="small"
            :predefine="predefineColors">
          </el-color-picker>
        </div>
        <!--        标题-->
        <div class="title">
          我的标签
        </div>
        <!--        列表-->
        <div class="list">
          <el-tag
            :style="{backgroundColor:color}"
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="$event.target.blur"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button :style="{backgroundColor:color}" v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
        </div>
      </div>
      <!--      每一项标签-->
      <div class="detail">
        <div class="color-select">
          <el-color-picker
            v-model="color"
            show-alpha
            size="small"
            :predefine="predefineColors">
          </el-color-picker>
        </div>
        <!--        标题-->
        <div class="title">
          喜欢的音乐
        </div>
        <!--        列表-->
        <div class="list">
          <el-tag
            :style="{backgroundColor:color}"
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="$event.target.blur"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button :style="{backgroundColor:color}" v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
        </div>
      </div>
      <!--      每一项标签-->
      <div class="detail">
        <div class="color-select">
          <el-color-picker
            v-model="color"
            show-alpha
            size="small"
            :predefine="predefineColors">
          </el-color-picker>
        </div>
        <!--        标题-->
        <div class="title">
          喜欢的食物
        </div>
        <!--        列表-->
        <div class="list">
          <el-tag
            :style="{backgroundColor:color}"
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="$event.target.blur"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button :style="{backgroundColor:color}" v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
        </div>
      </div>
      <!--      每一项标签-->
      <div class="detail">
        <div class="color-select">
          <el-color-picker
            v-model="color"
            show-alpha
            size="small"
            :predefine="predefineColors">
          </el-color-picker>
        </div>
        <!--        标题-->
        <div class="title">
          旅行的足迹
        </div>
        <!--        列表-->
        <div class="list">
          <el-tag
            :style="{backgroundColor:color}"
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="$event.target.blur"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button :style="{backgroundColor:color}" v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'myLabel',
    data() {
      return {
        dynamicTags: ['标签一', '标签二', '标签三','标签1','标签2','标签3','标签4','标签5','标签8'],
        inputVisible: false,
        inputValue: '', //标签输入值
        color: 'rgba(3,146,255)', //默认颜色
        predefineColors: [ //预选颜色
          '#ff4500',
          '#ff8c00',
          '#ffd700',
          '#90ee90',
          '#00ced1',
          '#1e90ff',
          '#c71585',
          'rgba(255, 69, 0, 0.68)',
          'rgb(255, 120, 0)',
          'hsv(51, 100, 98)',
          'hsva(120, 40, 94, 0.5)',
          'hsl(181, 100%, 37%)',
          'hsla(209, 100%, 56%, 0.73)',
          '#c7158577'
        ]
      };
    },
    methods: {
      //处理关闭标签事件
      handleClose(tag) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },
      //新增加标签
      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      }
    }
  }
</script>

<style lang="scss" scoped >
  ::v-deep .el-tag .el-tag__close {
    color: #ffffff;
    background-color: rgba(255,255,255,.2);
  }
  .app-container {
    .detail {
      position: relative;
      margin-bottom: 40px;
      .color-select{
        position: absolute;
        left: 0;
        top: 0;
      }
      .title {
          text-align: center;
        margin-bottom: 20px;
      }
      .list{
        padding: 0 20px;
        .el-tag {
          margin-left: 10px;
          margin-right: 13px;
          color: #fff;

        }
        .button-new-tag {
          margin-left: 10px;
          height: 32px;
          line-height: 30px;
          padding-top: 0;
          padding-bottom: 0;
          margin-top: 15px;
          color: #fff;
        }
        .input-new-tag {
          width: 90px;
          margin-left: 10px;
          vertical-align: bottom;
          color: #fff;

        }
      }
    }
  }
</style>
